package com.bcbim.api_model.sys.interceptor;

import com.alibaba.fastjson.JSON;
import com.bcbim.authentication_interface.application.bo.ApplicationBO;
import com.bcbim.authentication_interface.application.service.ApplicationService;
import com.bcbim.authentication_interface.util.ApplicationAuthUtil;
import com.bcbim.common.response.vo.ReturnMsg;
import com.bcbim.common.sys.Constant;
import com.bcbim.common.sys.ReturnStatus;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Description: 应用请求
 * @Author: ye
 * @Date: 2022/4/6
 * @Version: V1.0
 **/
@Component
public class ApplicationAuthenticationInterceptor implements HandlerInterceptor {

    @Reference(protocol = "dubbo", group = "application", version = "1.0.0")
    private ApplicationService applicationServiceImpl;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // options 请求直接放过
        if (request.getMethod().toLowerCase().equals("options")) {
            return Boolean.TRUE;
        }
        String url = request.getRequestURI();
        if (url.contains(Constant.RESOURCES_PATH)) {
            return Boolean.TRUE;
        }
        // 获取token
        String appKey = request.getParameter(Constant.KEY_PARAM);
        if (StringUtils.isEmpty(appKey)) {
            responseMsg(response,
                    HttpServletResponse.SC_UNAUTHORIZED,
                    ReturnMsg.consFail(ReturnStatus.UNAUTHORIZED.getCode(), Constant.APP_UNAUTHORIZED_MSG));
            return Boolean.FALSE;
        }

        //校验key 是否合法
        ApplicationBO applicationBO = applicationServiceImpl.checkAppKey(appKey);
        if (applicationBO == null) {
            responseMsg(response,
                    HttpServletResponse.SC_UNAUTHORIZED,
                    ReturnMsg.consFail(ReturnStatus.UNAUTHORIZED.getCode(), Constant.APP_UNAUTHORIZED_MSG));
            return Boolean.FALSE;
        }

        ApplicationAuthUtil.setApplication(applicationBO);
        return Boolean.TRUE;
    }

    /**
     * @Description: 返回客户端信息
     * @Param: response HttpServletResponse对象
     * @Param: responseStatus 系统返回码
     * @Param: returnMsg 返回对象
     * @Author: haoshuai
     * @Date: 2020/11/5
     */
    private void responseMsg(HttpServletResponse response, int responseStatus, ReturnMsg returnMsg) throws IOException {
        response.setStatus(responseStatus);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter out;
        out = response.getWriter();
        out.write(JSON.toJSON(returnMsg).toString());
        out.flush();
        out.close();
    }
}
